import FileSaver from "./FileSaver";

enum FileType {
    DATA_URL,
    TEXT,
    BINARY,
    ARRAYBUFFER,
}

class FileMgr {
    private static instance: FileMgr;

    public static get getInstance() {
        if (!FileMgr.instance) {
            FileMgr.instance = new FileMgr();
        }
        return FileMgr.instance;
    }

    public downloadFile(data: string, fileName: string, saveAs: boolean = false) {
        const file = new File([data], fileName, {
            type: "text/plain;"
        });
        if (saveAs) {
            return file;
        }
        FileSaver.saveAs(file);
    }

    public loadMaps(dir: string, callback: (maps: any[]) => void) {
        cc.loader.loadResDir(dir, (error, resources) => {
            if (error) {
                cc.error("loadMapData", error);
            } else {
                const maps = [];
                for (let i = 0; i < resources.length; i++) {
                    const mapJson = resources[i].json;
                    mapJson.name = resources[i]._name;
                    maps.push(mapJson);
                    console.log("加载地图数据成功", mapJson.name, mapJson);
                }
                callback(maps);
            }
        });
    }

    public readJsonFile(callback: (result: any, name: string) => void) {
        this.openLocalFile(".json", (file: File) => {
            console.log("file", file);
            this.readLocalFile(file, FileType.TEXT, (result: string) => {
                callback && callback(result, file.name);
            });
        });
    }

    public openLocalFile(accept: string, callback: (file: File) => void) {
        let inputElement = document.getElementById("file_input") as HTMLInputElement;
        if (!inputElement) {
            inputElement = document.createElement("input");
            inputElement.id = "file_input";
            inputElement.setAttribute("type", "file");
            inputElement.setAttribute("class", "fileToUpload");
            inputElement.style.opacity = "0";
            inputElement.style.position = "absolute";
            inputElement.setAttribute("left", "-999px");
            document.body.appendChild(inputElement);
        }
        inputElement.setAttribute("accept", accept);
        inputElement.onchange = () => {
            const files = inputElement.files;
            if (files && files.length > 0) {
                const file = files[0];
                callback && callback(file);
            }
        };
        inputElement.click();
    }

    public readLocalFile(file: File, fileType: FileType, callback: (result: any) => void) {
        const reader = new FileReader();
        reader.onload = () => {
            callback && (reader.readyState == FileReader.DONE ? callback(reader.result) : callback(null));
        };
        switch (fileType) {
            case FileType.DATA_URL:
                reader.readAsDataURL(file);
                break;
            case FileType.TEXT:
                reader.readAsText(file);
                break;
            case FileType.BINARY:
                reader.readAsBinaryString(file);
                break;
            case FileType.ARRAYBUFFER:
                reader.readAsArrayBuffer(file);
                break;
        }
    }

    public saveForBrowser(data: string, fileName: string) {
        if (cc.sys.isBrowser) {
            console.log("浏览器");
            const blob = new Blob([data], {
                type: "application/json"
            });
            const link = document.createElement("a");
            link.download = fileName;
            link.innerHTML = "Download File";
            if (window.webkitURL !== null) {
                link.href = window.webkitURL.createObjectURL(blob);
            } else {
                link.href = window.URL.createObjectURL(blob);
                link.style.display = "none";
                document.body.appendChild(link);
            }
            link.click();
        }
    }
}

const fileMgr = FileMgr.getInstance;
export default fileMgr;
